package com.tubang.logistics.controller;

import java.util.List;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.plugins.Page;
import com.tubang.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tubang.common.constant.Constants;
import com.tubang.common.util.ResultData;
import com.tubang.component.entity.CompanyEntity;
import com.tubang.component.entity.UserEntity;
import com.tubang.component.service.ICompanyService;
import com.tubang.component.service.IUserService;

/**
 * <p>
 * 组织表 部门人员 前端控制器
 * </p>
 *
 * @author maxl
 * @since 2017-11-30
 */
@RestController
@RequestMapping("/dispatchUser")
public class CompanyUserController {
	private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private IUserService userService;
    @Resource
	private ICompanyService companyService;
    
    /**
     * @Title getCompanyByType 
     * @Description 获取公司列表根据公司类型
     * @param type
     * @param
     * @return ResultData 返回类型 
     * @throws
     */
    @GetMapping(value = "getCompanyByType")
    public ResultData getCompanyByType(@RequestParam(required=true) String type,
                                       @RequestParam(required = false, defaultValue = "1") Integer page,
                                       @RequestParam(required = false, defaultValue = "10") Integer rows) {
        try {
			EntityWrapper<CompanyEntity> companyEntityWrapper = new EntityWrapper<CompanyEntity>();
			companyEntityWrapper.eq("type",type);
			companyEntityWrapper.eq("enable", Constants.DEFAULT_VAULE_ONE);
            Page<CompanyEntity> companyEntityPage = companyService.selectPage(new Page<CompanyEntity>(page, rows), companyEntityWrapper);
            int count = companyService.selectCount(companyEntityWrapper);
            return ResultData.result(true).setData(companyEntityPage.getRecords()).setTotalCount(count);
        } catch (Exception e) {
        	e.printStackTrace();
			logger.error("[getUserByCompanyId]==> method[getUserByCompanyId],错误原因：[{}]", e.getMessage());
            return ResultData.result(false);
        }
    }

    /**
     * @Title getUserByUserType 
     * @Description 根据用户类型查询用户列表
     * @param userType
     * @param
     * @return ResultData 返回类型 
     * @throws
     */
    @GetMapping(value = "getUserByUserType")
    public ResultData getUserByUserType(@RequestParam(required = true) String userType,
                                        @RequestParam(required = false) String userName,
                                        @RequestParam(required = false, defaultValue = "1") Integer page,
                                        @RequestParam(required = false, defaultValue = "10") Integer rows) {
        try {
        	EntityWrapper<UserEntity> wrapper = new EntityWrapper<UserEntity>();
        	wrapper.eq("user_type", userType);
        	if (StringUtils.isNotEmpty(userName)){
                wrapper.eq("name", userName);
            }

            Page<UserEntity> userEntityPage = userService.selectPage(new Page<UserEntity>(page, rows), wrapper);
            int count = userService.selectCount(wrapper);
            return ResultData.result(true).setData(userEntityPage.getRecords()).setTotalCount(count);
        } catch (Exception e) {
        	e.printStackTrace();
			logger.error("[getUserByCompanyInfo]==> method[getUserByCompanyInfo],错误原因：[{}]", e.getMessage());
            return ResultData.result(false).setData(e);
        }
    }

}
